package com.jing.Recyclerview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Adapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.jing.Recyclerview.adapter.FruitRecyclerAdapter;
import com.jing.Recyclerview.callback.LoadMoreCallback;
import com.jing.Recyclerview.entity.Student;
import com.jing.Recyclerview.listener.EndlessScrollListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements LoadMoreCallback{

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<Student> students;
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        students=new ArrayList<>();

        students.add(new Student("张三", 21, "男"));
        students.add(new Student("李四", 21, "女"));
        students.add(new Student("王五", 21, "男"));
        students.add(new Student("赵六", 22, "男"));
        students.add(new Student("孙七", 22, "女"));
        students.add(new Student("周八", 22, "男"));
        students.add(new Student("吴九", 23, "男"));
        students.add(new Student("郑十", 23, "女"));
        students.add(new Student("冯十一", 23, "男"));
        students.add(new Student("钱十二", 24, "男"));
        students.add(new Student("陈十三", 24, "女"));
        students.add(new Student("褚十四", 24, "男"));
        students.add(new Student("卫十五", 25, "男"));
        students.add(new Student("邹十六", 25, "女"));
        students.add(new Student("杨十七", 25, "男"));
        students.add(new Student("黄十八", 26, "男"));
        students.add(new Student("朱十九", 26, "女"));
        students.add(new Student("毛二十", 26, "男"));
        students.add(new Student("陆二十一", 27, "男"));
        students.add(new Student("宋二十二", 27, "女"));
        students.add(new Student("唐二十三", 27, "男"));
        students.add(new Student("阮二十四", 28, "男"));
        students.add(new Student("董二十五", 28, "女"));
        students.add(new Student("袁二十六", 28, "男"));
        students.add(new Student("崔二十七", 29, "男"));
        students.add(new Student("霍二十八", 29, "女"));
        students.add(new Student("毛二十九", 29, "男"));
        students.add(new Student("林三十", 30, "男"));
        students.add(new Student("蒋三十一", 30, "女"));
        students.add(new Student("冯三十二", 30, "男"));

        recyclerView = findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        //https://www.jianshu.com/p/26d9515d3e2d

//        recyclerView.setLayoutManager(new GridLayoutManager(this,2,RecyclerView.VERTICAL,false));
        adapter =  new FruitRecyclerAdapter(this,students);
        recyclerView.setAdapter(adapter);

        // 添加系统默认的横向分隔线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
                recyclerView.getContext(),
                DividerItemDecoration.VERTICAL
        );
        recyclerView.addItemDecoration(dividerItemDecoration);


        DefaultItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setAddDuration(1000); // 设置添加动画的持续时间
        itemAnimator.setRemoveDuration(1000); // 设置移除动画的持续时间

        recyclerView.setItemAnimator(itemAnimator);
        swipeRefreshLayout=findViewById(R.id.swipeRefreshLayout);
        // 设置 SwipeRefreshLayout 的刷新监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 这里进行下拉刷新操作
                refreshData();
            }
        });

        // 初始化并添加滚动监听器
        scrollListener = new EndlessScrollListener((LinearLayoutManager) recyclerView.getLayoutManager(), this);
        recyclerView.addOnScrollListener(scrollListener);
    }
    private void refreshData() {
        // 模拟网络请求或数据加载
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                students.clear();
                loadData(); // 重新加载数据
                swipeRefreshLayout.setRefreshing(false); // 结束刷新
            }
        }, 2000); // 模拟延迟2秒
    }
    private void loadData() {
        // 模拟加载数据
        for (int i = 0; i < 20; i++) {
            students.add(new Student("姓名"+(i+1),20+(i%5),i%2==0?"男":"女"));
        }
        adapter.notifyDataSetChanged();
    }

    private EndlessScrollListener scrollListener;
    private boolean hasMoreData = true; // 控制是否还有更多数据

    @Override
    public void onLoadMore() {
        if (hasMoreData) {
            // 模拟加载更多数据
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadMoreData();
                    scrollListener.setLoading(false); // 加载完成
                }
            }, 1000); // 模拟延迟2秒
        }
    }
    private void loadMoreData() {
        // 模拟加载更多数据
        int start = students.size();
        for (int i = start; i < start + 10; i++) {
            students.add(new Student("更多  -----  姓名"+(i+1),20+(i%5),i%2==0?"男":"女"));
        }
        adapter.notifyDataSetChanged();

        // 控制是否还有更多数据
        if (students.size() > 100) { // 假设100个数据后就没有更多数据了
            hasMoreData = false;
        }
    }
}